'''
Created on Jan 20, 2011

@author: yc30v1m
'''
import xmlrpclib

#from plugins.Log import log

class xmlRpcConnector(object):
    '''
    xmlRpcConnector Class connects to Client and fires the query and get the results done
    '''
    connectorUrl = "http://localhost:4444"
    sDriverName = "" 
    sDataBaseName = ""
    sHost = ""
    sPort = ""
    sUserName = ""
    sPassword = ""
    rows_count = 0
    columns_count = 0
    __isConnected = False
    _rows =""
#    log = Log().getLogger("xmlRpcConnector")

    def __init__(self):
        self.setConnectionUrl()
        # Disabled right now as we are picking up from INI files
        #self._proxy = xmlrpclib.ServerProxy(self.connectorUrl, transport=None, encoding=None, verbose=0, allow_none=0, use_datetime=1)
        
    def connectTo(self, sDriverName, sDataBaseName, sHost, sUserName, sPassword, sPort = "",sSchema = ""):
        self.sDriverName = sDriverName
        self.sDataBaseName = sDataBaseName
        self.sHost = sHost
        self.sPort = sPort
        self.sUserName = sUserName
        self.sPassword = sPassword
    
    def executeQuery(self, sQuery):
        '''
        "pg","bdc_oz","11.16.155.55","bcom_user1","bcom_user","select * from kst limit 10"
        '''
        try:
            
            self._rows = self._proxy.xmlrpcConnector.dbExec(self.sDriverName, self.sDataBaseName, self.sHost, self.sUserName, self.sPassword, sQuery,self.sPort)
                   
            self.log.debug("Connected using : " + str(self.sDriverName) + "  " + str(self.sDataBaseName) + "  " + str(self.sHost) + "  " + str(self.sUserName) + "  " + str(sQuery) + "  " + str(self.sPort))
            self.__isConnected = True
        except Exception as e:
            
            self.__isConnected = False
            self.log.debug("Error occured Connecting to DB in Connector.py::executeQuery " +str(e))
            self.log.debug( "Connected using : " + self.sDriverName + "  " + self.sDataBaseName + "  " + self.sHost + "  " + self.sUserName + "   " +sQuery + "  " +self.sPort)
        return self._rows
    
    '''
    Get the count of Rows from results
    '''
    def rowCount(self):
        self.rows_count = 0
        try:
            if self.__isConnected:
                self.rows_count = len(self._rows)
        except Exception as e:
            self.log.debug("Exeception Printing Backtrace ")
            print str(e)
        return self.rows_count
    
    '''
    Get the count of Columns from results
    '''
    def columnCount(self):
        self.columns_count = 0
        try:
            if self.__isConnected :
                self.columns_count = len(self._rows[0])
        except Exception as e:
            self.log.debug("Exeception Printing Backtrace ")
            print str(e)
        return self.columns_count
    
    def connectorStatus(self):
        return self.__isConnected
    
    def setConnectionUrl(self,url = 'http://localhost:4444'):
        self.log.debug('xmlrpc Db Connector url : ',url)
        self.connectorUrl = url
        if self.connectorUrl:
            self._proxy = xmlrpclib.ServerProxy(self.connectorUrl, transport=None, encoding=None, verbose=0, allow_none=0, use_datetime=1)
        else:
            self.log.critical('The XMLRPC Connection Url is not set')
            
    ''' 
    Not implemented methods Will throws Exceptions
    '''
    def hasNext(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
    
    def getCurrentRow(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")

    def getRow(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
    
    def getColumnNames(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
    
    def getRowValuesAsList(self, row):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
    
    def getCurrentRowValuesAsList(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
    
    def getCurrentRowAsDict(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")

    def getColumnValueByName(self, colName):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
        
    def getColumnValueById(self, id):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
    
    def getNextRow(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
        
    def moveNext(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
            
    def rewind(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
    
    def getFirstRow(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")
        
    def getLastRow(self):
        raise Exception("This Function Not Implemented in xmlRpcConnector")